// userPageB/pages/creditCardSear/creditCardSear.js
const app = getApp()
const WXAPI = require("../../../api/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '预付卡查询', // 导航栏 中间的标题
      textColor: '#FFFFFF', // 标题颜色
      bgColor: '#F75D0B', // 导航栏背景颜色
      isloading: false,
    },
    // 此页面 页面内容距最顶部的距离
    imgHttp: app.globalData.imgHttp,
    getCodeText: '获取验证码',
    countdown: 60,
    timer: '',//定时器名字
    isCodeStatus: 0,
    cardId: '',
    phone: '',
    code: '',
    loadText: "输入账号无效",
    is_btn_active: false,
    isSuccess: false,
    cardDetail: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setNavSize()
  },
  //拿到卡号
  getCardId: function (e) {
    var val = e.detail.value;
    console.log('val',val)
    this.setData({
      cardId: val
    });
  },   
  //拿到手机号
  getPhone: function (e) {
    var val = e.detail.value;
    console.log('val',val)

    this.setData({
      phone: val
    });
  },   
  // 拿到验证码
  getCode: function (e) {
      var val = e.detail.value;
      if(val.length >= 6){
        this.setData({
          is_btn_active: true,
        })
      }
      this.setData({
        code: val
      });
  },
  countdownFun(){
    let that = this;
    var phone = this.data.phone;
    var phonetel = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var name = /^[u4E00-u9FA5]+$/;
    if (phone === '') {
      that.setData({
        loadText: '手机号不能为空',
      })
      this.showDialog();
      return false
    }else if (phone.length != 11) {
      that.setData({
        loadText: '手机号长度有误',
      })
      this.showDialog();
      return false;
    }else if (!phonetel.test(phone)) {
      that.setData({
        loadText: '手机号码错误',
      })
      this.showDialog();
      return false;
    }
    this.btnCode()
  },
  btnCode(){
    let that = this;
    let countdown = that.data.countdown;
    let isCodeStatus = that.data.isCodeStatus;
    
    var data = {
      phone: this.data.phone

    }
    WXAPI.getCreditCode( data ,(res) =>{
      console.log("获取验证码",res)
      if(res.success){
        if(isCodeStatus === 0 || isCodeStatus === 2){
          that.setData({
            isCodeStatus: 1
          })
        }
        this.setData({
          timer: setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
            that.data.countdown--;
            that.setData({
              countdown: that.data.countdown
            })
            if (that.data.countdown == 0) {
              clearInterval(that.data.timer);
              that.setData({
                isCodeStatus: 2,
                countdown:60,
                getCodeText: '重新获取验证码'
              })
            }
          }, 1000)
        })
        }else{

        }
    })
    
  },
  //诚信卡查询
  getList(){
    var data = {
      cardId: this.data.cardId,
      userName: this.data.phone,
      code: this.data.code,
    }
    WXAPI.creditCardSearList( data ,(res) =>{
      console.log("获取验证码",res)
      if(res.code == 200){
        var data = res.result
        console.log('诚信卡列表',res)
        this.setData({
          isSuccess: true,
          cardDetail: data,
        })
      }
    })
  },
  back(){
    this.setData({
      isSuccess: false,
      cardId: '',
      phone: '',
      isCodeStatus: 0,
      getCodeText: '获取验证码',
    })
  },
  showDialog() {
    var title = this.data.loadText
    wx.showToast({
      title: title,
      icon:'none'
    })
  },
  // 通过获取系统信息计算导航栏高度
  setNavSize: function() {
    var that = this
        , sysinfo = wx.getSystemInfoSync()
        , statusHeight = sysinfo.statusBarHeight
        , isiOS = sysinfo.system.indexOf('iOS') > -1
        , navHeight;
    var height = wx.getSystemInfoSync().windowHeight
    
    if (!isiOS) {
        navHeight = 48;
    } else {
        navHeight = 44;
    }
    height =  height-statusHeight-navHeight
    console.log('height',height)
    console.log('statusHeight',statusHeight)
    console.log('navHeight',navHeight)
    that.setData({
        status: statusHeight,
        navHeight: navHeight,
    })
  },
  //展示图片
  showImg:function(e){
    // https://bee.xiaodaiyan.com/group1/M00/00/04/rBAAD2FNrOKAQB8XAAKi-7ypdQs467.jpg
    var that = this
    var imgList = []
    imgList.push(this.data.imgHttp+this.data.cardDetail.businessLicenseImg)
    wx.previewImage({
        urls: imgList,
        current: this.data.imgHttp+that.data.cardDetail.businessLicenseImg
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})